package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.Setmeal;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetMealDishMapper;
import com.sky.mapper.SetMealMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * Description:保存菜品和口味
 */
@Service
public class DishServiceImpl implements DishService {

    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private SetMealMapper setMealMapper;
    @Autowired
    private SetMealDishMapper setMealDishMapper;

    /**
     * 添加菜品
     *
     * @param dishDTO
     * @return
     */
    @Transactional
    public void saveWithFlavor(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        //向菜品表里面插入数据
        dishMapper.insert(dish);

        //获取新添加的菜品id
        Long dishId = dish.getId();

        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors != null && flavors.size() > 0) {
            flavors.forEach(dishFlavor -> {
                dishFlavor.setDishId(dishId);
            });
            //向口味表里面插入数据
            dishFlavorMapper.insertBatch(flavors);
        }
    }

    /**
     * 分页查询菜品
     *
     * @param dishPageQueryDTO
     * @return
     */
    public PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO) {
        //开启分页
        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());

        Page<DishVO> page = dishMapper.page(dishPageQueryDTO);
        //从page对象里面获取总记录数和菜品集合
        long total = page.getTotal();
        List<DishVO> records = page.getResult();

        return new PageResult(total, records);
    }

    /**
     * 批量删除菜品
     *
     * @param ids
     */
    @Transactional
    public void batchRemove(List<Long> ids) {
        for (Long id : ids) {
            //查询菜品是否正在售卖
            Dish dish = dishMapper.getDishStatus(id);
            if (dish.getStatus() == StatusConstant.ENABLE) {
                //如果菜品正在启售，则不允许删除
                throw new DeletionNotAllowedException("菜品正在售卖中,不允许删除");
            }
        }
        //查询菜品是否关联了套餐
        List<Long> setMealDishInfo = setMealDishMapper.getSetMealDishInfo(ids);
        if (setMealDishInfo != null && setMealDishInfo.size() > 0) {
            //如果菜品关联了套餐，则不允许删除
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);
        }
        /*for (Long id : ids) {
            //对菜品进行批量删除
            dishMapper.batchDelete(id);
            //删除菜品的同时也要把关联的菜品口味删除
            dishFlavorMapper.deleteDishFlavor(id);
        }*/
        //对菜品进行批量删除
        dishMapper.batchDeleteIds(ids);
        //删除菜品的同时也要把关联的菜品口味删除
        dishFlavorMapper.deleteDishFlavorIds(ids);
    }

    /**
     * 根据菜品ID查询菜品信息
     *
     * @param id
     * @return
     */
    public DishVO queryDishById(Long id) {
        //根据菜品ID查询菜品信息
        Dish dish = dishMapper.getDishById(id);
        //根据菜品ID查询菜品口味信息
        List<DishFlavor> dishFlavor = dishFlavorMapper.getDishFlavorById(id);
        //将查询到的数据封装到VO
        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish, dishVO);
        dishVO.setFlavors(dishFlavor);

        return dishVO;
    }

    /**
     * 修改菜品
     *
     * @param dishDTO
     * @return
     */
    @Transactional
    public void modify(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        //修改菜品表
        dishMapper.update(dish);
        //先删除口味表中的数据
        dishFlavorMapper.deleteDishFlavor(dishDTO.getId());
        //在进行添加新的口味
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors != null && flavors.size() > 0) {
            flavors.forEach(dishFlavor -> {
                dishFlavor.setDishId(dishDTO.getId());
            });
            //向口味表里面插入数据
            dishFlavorMapper.insertBatch(flavors);
        }
    }

    /**
     * 菜品的启售和停售
     *
     * @param status
     * @return
     */
    @Transactional
    public void startOrStop(Integer status, Long id) {
        Dish dish = Dish.builder()
                .status(status)
                .id(id)
                .build();

        dishMapper.update(dish);
        //如果是停售操作，还需要将包含当前菜品的套餐也停售
        if (status == StatusConstant.DISABLE) {
            //使用批量删除的方法需要List集合,所以把ID放到集合里面进行传递
            List<Long> idList = new ArrayList<>();
            idList.add(id);
            //查询有多少套餐包含了这个菜品
            List<Long> setMealDishIds = setMealDishMapper.getSetMealDishInfo(idList);
            if (setMealDishIds != null && setMealDishIds.size() > 0) {
                //把查询到的套餐包含了这个菜品的ID一个一个取出来进行停售
                for (Long setMealDishId : setMealDishIds) {
                    Setmeal setmeal = Setmeal.builder()
                            .id(setMealDishId)
                            .status(StatusConstant.DISABLE)
                            .build();
                    //进行修改停售
                    setMealMapper.update(setmeal);
                }
            }
        }
    }

    /**
     * 根据分类id查询菜品
     *
     * @param categoryId
     * @return
     */
    public List<Dish> dishList(Long categoryId, String name) {
        Dish dish = Dish.builder()
                .name(name)
                .categoryId(categoryId)
                .status(StatusConstant.ENABLE)
                .build();

        return dishMapper.getDishList(dish);
    }

    /**
     * 条件查询菜品和口味
     *
     * @param dish
     * @return
     */
    public List<DishVO> listWithFlavor(Dish dish) {

        List<Dish> dishList = dishMapper.getDishList(dish);

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d, dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorMapper.getDishFlavorById(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }
}
